package com.suge.home.module.screen.json;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.suge.home.module.screen.Index;
import com.suge.order.dao.UserBizOrderDAO;
import com.suge.order.dao.dataobject.UserBizOrderDO;

public class AddNoSendBuyer {

	private static final Logger logger = LoggerFactory.getLogger(AddNoSendBuyer.class);

	@Autowired
    private HttpServletRequest request;
	@Autowired
	private UserBizOrderDAO userBizOrderDAO;
	public void execute(Context context, Navigator nav) {
		String userId = request.getParameter("userId");
		if(userId==null || userId==""){
			context.put("isSuccess", false);
			return;
		}
		long userIdNum =  Long.parseLong(userId);
		String buyerList = request.getParameter("buyers");
		UserBizOrderDO userBizorderDO = userBizOrderDAO.getUserBizOrderByUserId(userIdNum);
		if(userBizorderDO==null || buyerList=="" || buyerList==null){
			logger.error("user biz order is null!");
		}
		else{
			logger.error("user biz order is  not null!");
			String noSend = userBizorderDO.getNoSendUsers();
			if(noSend==null || noSend==""){
				noSend=buyerList;
			}
			else{
				String[] nicks = noSend.split(";");
				String[] adds = buyerList.split(";");
				for(String add:adds){
					boolean isExisted = false;
					String addNow =add;
					try {
						if(add!=null && add!=""){
							addNow = URLDecoder.decode(add, "UTF-8");	
						}		
					} catch (Exception e) {
					}
					for(String nick:nicks){
						if(addNow.equals(nick)){
							isExisted=true;
						}
					}
					if(!isExisted){
						noSend=addNow+";"+noSend;
					}
				}		
			}
			userBizorderDO.setNoSendUsers(noSend);
			userBizOrderDAO.updateUserBizOrderInfo(userBizorderDO);
		}
		context.put("isSuccess", true);
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setUserBizOrderDAO(UserBizOrderDAO userBizOrderDAO) {
		this.userBizOrderDAO = userBizOrderDAO;
	}

	public UserBizOrderDAO getUserBizOrderDAO() {
		return userBizOrderDAO;
	}

}
